import java.util.*;
import java.io.*;

class AMCParser
{
    /*public static void main(String[] args)
    {
        AMCParser program = new AMCParser();
        ArrayList<Motion> m = program.parseFile("07_06.amc");
        program.outputFile(m, "amcClone.amc");
    }*/
    
    public void outputFile(ArrayList<Motion> motion, String filename)
    {
        FileOutputStream out;
        PrintStream p;
        try
        {
                out = new FileOutputStream(filename);
                p = new PrintStream( out );
                p.println(":FULLY-SPECIFIED");
                p.println(":DEGREES");
                for (int i = 0; i < motion.get(0).getWave(0).length; i++)
                {
                    p.println(i + 1);
                    Motion m;
                    double[] f;
                    for (int j = 0; j < motion.size(); j++)
                    {
                        m = motion.get(j);
                        p.print (m.name());
                        f = m.getFrame(i);
                        for (int k = 0; k < f.length; k++)
                            p.print(" " + f[k]);
                        p.println("");
                    }
                }
                p.close();
        }
        catch (Exception e)
        {
                System.err.println ("Error writing to file");
                e.printStackTrace();
        }
    }
    
    private String[] getFile(String filename)
    {
       File file = new File(filename);
       String line;
       ArrayList<String> lineList;
       lineList = new ArrayList<String>();
       try
       {
          Scanner input = new Scanner(file);
          while (input.hasNextLine())
          {
             line = input.nextLine();
             lineList.add(line);
          }
          input.close();
       }
       catch (IOException err)
       {
          fail("Error whilst reading file.");
       }
       String[] lines = new String[lineList.size()];
       lineList.toArray(lines);
       return lines;
    }
    
    private void fail(String msg)
    {
       System.err.println("Error: " + msg);
       System.exit(1);
    }
    
    public ArrayList<Motion> parseFile(String filename)
    {
        return parseLines(getFile(filename));
    }
    
    private ArrayList<Motion> parseLines(String[] lines)
    {
        ArrayList<Motion> motions = new ArrayList<Motion>();
        for (int i = 0; i < lines.length; i++)
        {
            try
            {
                Integer.parseInt(lines[i]);
            }
            catch(Exception e)
            {
                if (!(lines[i].startsWith("#") || lines[i].startsWith(":")))
                {
                    String[] line = lines[i].split(" ");
                    motions = updateMotion(line[0], parseMotion(removeHead(line)), motions);
                }
            }
        }
        return motions;
    }
    
    private ArrayList<Motion> updateMotion(String name, double[] motion, ArrayList<Motion> motions)
    {
        int i = getMotionIndex(name, motions);
        if (i == -1)
        {
            motions.add(new Motion(name));
            i = motions.size() - 1;
        }
        motions.get(i).addMotion(motion);
        return motions;
    }
    
    private int getMotionIndex(String name, ArrayList<Motion> motions)
    {
        for (int i = 0; i < motions.size(); i++)
        {
            if (motions.get(i).name().equals(name))
                return i;
        }
        return -1;
    }

      public Motion getMotion(String name, ArrayList<Motion> motions)
    {
        int mIndex = getMotionIndex(name,motions);

        if (mIndex != -1)
        {
            return motions.get(mIndex);
        }
        else
            return null;
    }

    public double[] getMotionFrame(String name, ArrayList<Motion> motions, int index)
    {
        Motion a = this.getMotion(name,motions);
        if (a != null)
        {
            return a.getFrame(index);
        }
        else
            return null;
    }

    private double[] parseMotion(String[] m)
    {
        double[] motion = new double[m.length];
        for (int i = 0; i < m.length; i++)
            motion[i] = Double.parseDouble(m[i]);
        return motion;
    }
    
    private String[] removeHead(String[] line)
    {
       String[] x = new String[line.length-1];
       for (int i = 1; i < line.length; i++)
          x[i-1] = line[i];
       return x;
    }
}
